package cn.janescott.inner.space.webapp;

import cn.janescott.inner.space.webapp.adapter.DatabaseRealmHandler;
import com.alibaba.dubbo.spring.boot.DubboConsumerAutoConfiguration;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.apache.shiro.web.filter.PathMatchingFilter;
import org.apache.shiro.web.filter.authc.AuthenticatingFilter;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.filter.mgt.FilterChainResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author scott
 * @date 2018/7/25
 */
@EnableDubboConfiguration
@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class WebappApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebappApplication.class);
    }

    @Bean
    public BeanPostProcessor getBeanPostProcessor() {
        return new BeanPostProcessor() {
            private Logger logger = LoggerFactory.getLogger(getClass());

            @Override
            public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof RedisTemplate) {
                    logger.info("打印对象" + beanName + "!");
                }

                if (bean instanceof FilterChainResolver) {
                    logger.info("shiro filter 解析器");
                }

                if (bean instanceof PathMatchingFilter) {
                    logger.info("过滤器");
                }

                if (bean instanceof FormAuthenticationFilter) {
                    logger.info("表单过滤器");
                }

                if (bean instanceof DubboConsumerAutoConfiguration) {
                    logger.info("dubbo 消费者");
                }

                if (bean instanceof DatabaseRealmHandler) {
                    logger.info("shiro验证处理器");
                }
                return bean;
            }

            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof AuthenticatingFilter) {
                    logger.info("bean post processor: after initialization: bean: " + bean + "; beanName: " + beanName + "!");
                }
                return bean;
            }
        };
    }
}

